#ifndef __FOC_H
#define __FOC_H
#include "stdint-gcc.h"
#include "pid.h"
#include "tim.h"

#define MOTOR_DIS       0
#define MOTOR_EN        1

#define SVPWM_SQRT3 1.73205f
#define SVPWM_SQRT3_2 0.866f
#define SVPWM_TIMER htim3

typedef struct
{
    struct 
    {
        uint8_t enable; // enable motor
        uint8_t align;  //align at start
        float tar_id;   
        float tar_iq;
        float tar_speed;
        float tar_toq;
    }status;

    struct 
    {
        uint8_t pole_pair;      //motor pole pair
        float e_angle;          // electric angle = mechanical angle * pole pair
        float m_angle;          // mechenical angle
        float radian;           // electirc radian
        int32_t count;          // tim_count every nvic form pwm trigger timer
        uint8_t z_flag;         // get encoder z
        uint16_t offset;        // form latched phase to encoder z
        uint8_t dir;            // motor dir
    }encoder;

    union 
    {
        uint8_t data;
        struct 
        {
           uint8_t error1 : 1;//
           uint8_t error2 : 1;
           uint8_t error3 : 1;
           uint8_t error4 : 1;
           uint8_t error5 : 1;
           uint8_t error6 : 1;
           uint8_t error7 : 1;
           uint8_t error8 : 1;
        }bits;
        
    }error_status;

    motor_pid id_pid;
    motor_pid iq_pid;
    motor_pid spd_pid;
    motor_pid pos_pid;
    
    struct 
    {
        float ia;       //current phase a
        float ib;       //current phase b
        float ic;       //current phase c
        float ialpha;   //clark transfor
        float ibeta;    //clark transfor
        float id;       //park
        float iq;
        float ualpha;   //Valpha after clarc convert
        float ubeta;    //Vbeta
        float max_ts;   //max duration time of up bridge
        float adc_ts;   //stable time for adc when all down bridge is ipen
        float ts;       //pwm frequence time
        float udc;      //voltage of v bus
        uint8_t sector;
        // uint16_t adc[6];
        float ta;       //pwm duration
        float tb;       //pwm duration
        float tc;       //pwm duration
        float t0;       //seven parts of pwm vector
        float t1;       //seven parts of pwm vector
        float t2;       //seven parts of pwm vector
        float t3;       //seven parts of pwm vector
        float t4;       //seven parts of pwm vector
        float t5;       //seven parts of pwm vector
        float t6;       //seven parts of pwm vector
        float t7;       //seven parts of pwm vector
        float tx;       //sector combination of tx
        float ty;       //sector combination of ty
        float td;       //trigger source of get adc current value
        float u1;       //svpwm section judgement and duration calulate
        float u2;       //svpwm section judgement and duration calulate
        float u3;       //svpwm section judgement and duration calulate
    }svpwm;

    struct 
    {
        void(*set_ch_pwm)(TIM_HandleTypeDef *htim, uint8_t ch, float time); //set timer ccr value
    }func;

} motor, *p_motor;


void set_ch_pwm(TIM_HandleTypeDef *htim,uint8_t ch, float time);
void clark_trans(p_motor motor);
void park_trans(p_motor motor);
void anti_park(p_motor motor, float id, float iq);
void sector_judgment(p_motor motor);
void pwm_duration_calc(p_motor motor);
void motor_stop(void);
void pwm_out(p_motor motor);


#endif


